package net.pimpas.validation.result.impl;

import java.util.ArrayList;
import java.util.List;

import net.pimpas.validation.ValidationType;
import net.pimpas.validation.message.ValidationMessage;
import net.pimpas.validation.result.ValidationResult;

public class DefaultValidationResult implements ValidationResult {
	
	private List<ValidationMessage> messages;
	
	public DefaultValidationResult() {
		this.setMessages(new ArrayList<ValidationMessage>());
	}

	public List<ValidationMessage> getErrors() {
		return this.getByType(ValidationType.ERROR);
	}
	
	private List<ValidationMessage> getByType(ValidationType type) {
		List<ValidationMessage> result = new ArrayList<ValidationMessage>();
		for(ValidationMessage msg: this.getMessages())
			if(msg.getType().equals(type))
				result.add(msg);
		
		return result;
	}

	public List<ValidationMessage> getInfos() {
		return this.getByType(ValidationType.INFO);
	}

	public List<ValidationMessage> getMessages() {
		return this.messages;
	}

	public List<ValidationMessage> getWarns() {
		return this.getByType(ValidationType.WARN);
	}

	public boolean hasErrors() {
		List<ValidationMessage> t = this.getByType(ValidationType.ERROR);
		return t.size()>0;
	}

	public boolean hasInfos() {
		List<ValidationMessage> t = this.getByType(ValidationType.INFO);
		return t.size()>0;
	}

	public boolean hasWarns() {
		List<ValidationMessage> t = this.getByType(ValidationType.WARN);
		return t.size()>0;
	}

	public void setMessages(List<ValidationMessage> messages) {
		this.messages = messages;
	}

	public void addMessage(ValidationMessage message) {
		this.getMessages().add(message);
	}
}